<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>9.静态属性练习之课程管理类</title>
</head>

<body>
  <script>
    // 对单个对象操作不适用静态方法，只对单个对象操作的使用静态方法
    const data = [
      { name: "js", price: 100 },
      { name: "mysql", price: 212 },
      { name: "vue.js", price: 98 }
    ];
    class Lesson {
      constructor(data) {
        this.model = data;
      }
      // 访问器获取元素的价格，不用写直接用 .price 就可以调用了不用写()
      get price () {
        return this.model.price;
      }
      //模型方法，获得元素name
      name () {
        return this.model.name;
      }
      // 总价
      static totalPrice (data) {
        return data.reduce((t, c) => t + c.price, 0)
      }
      //批量生成对象
      static createBatch (data) {
        return data.map(item => new Lesson(item));
      }
      //最贵的课程
      static MaxPrice (collection) {
        return collection.sort((a, b) => b.price - a.price)[0];
      }
    }
    const lessons = Lesson.createBatch(data);
    console.log(lessons);
    console.log(Lesson.MaxPrice(lessons).name());
    console.log(Lesson.totalPrice(lessons));
  </script>

</body>

</html>